package chapter15;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 *
 * @author lzy
 * @create 2018-03-26 下午4:40
 */
public class EmpDao extends AbstractDao<Emp> {
    public EmpDao(Connection connection) {
        super(connection);
    }

    @Override
    public boolean insert(Emp emp) {
        Class<?> clazz = Emp.class;
        String sql = SQLHelper.getInsertSql(clazz);
        try {
            PreparedStatement stmt = connection.prepareStatement(sql);
            Object[] params = getObjectAttributes(emp);
            setParams(stmt, params);
            return stmt.executeUpdate() == 1;
        } catch (SQLException | NoSuchFieldException | IllegalAccessException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public Emp selectOne(String condition, Object... params) {
        String limitCondition = condition.trim() + (" LIMIT 1");
        String sql = SQLHelper.getConditionSelectSql(Emp.class, limitCondition);
        try {
            PreparedStatement stmt = connection.prepareStatement(sql);
            setParams(stmt, params);
            ResultSet rs = stmt.executeQuery();
            List<Emp> result = getDoFromPo(rs, Emp.class);
            if (!result.isEmpty()) {
                return result.get(0);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public List<Emp> selectAll() {
        String sql = SQLHelper.getSelectSql(Emp.class);
        List<Emp> result = new ArrayList<>();
        try {
            PreparedStatement stmt = connection.prepareStatement(sql);
            ResultSet rs = stmt.executeQuery();
            result = getDoFromPo(rs, Emp.class);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public boolean update(Emp emp, String condition, Object... params) {
        String sql = SQLHelper.getUpdateSql(Emp.class, condition);
        try {
            PreparedStatement stmt = connection.prepareStatement(sql);
            Object[] attributes = getObjectAttributes(emp);
            Object[] allparams = new Object[attributes.length + params.length];
            System.arraycopy(attributes, 0, allparams, 0, attributes.length);
            System.arraycopy(params, 0, allparams, attributes.length, params.length);
            setParams(stmt, allparams);
            return stmt.executeUpdate() == 1;
        } catch (SQLException | IllegalAccessException | NoSuchFieldException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean del(String condition, Object... params) {
        String sql = SQLHelper.getDelSql(Emp.class, condition);
        try {
            PreparedStatement pstmt = connection.prepareStatement(sql);
            setParams(pstmt, params);
            return pstmt.executeUpdate() == 1;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
}
